package academia.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.SystemColor;
import java.awt.TextField;
import javax.swing.JLabel;

import academia.negocio.ConverteTipo;
import academia.persistencia.PseudoBanco;

import com.jgoodies.forms.factories.DefaultComponentFactory;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Window.Type;
import javax.swing.SwingConstants;

public class TelaBusca extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */


	/**
	 * Create the frame.
	 */
	public TelaBusca() {
		setType(Type.POPUP);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 325, 160);
		contentPane = new JPanel();
		contentPane.setBackground(SystemColor.inactiveCaption);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		final TextField textCpf = new TextField();
		textCpf.setBounds(104, 37, 117, 22);
		contentPane.add(textCpf);
		
		final JLabel lblCpf = DefaultComponentFactory.getInstance().createLabel("CPF");
		lblCpf.setForeground(SystemColor.text);
		lblCpf.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblCpf.setBounds(56, 37, 42, 22);
		contentPane.add(lblCpf);
		
		final JLabel lblStatus = DefaultComponentFactory.getInstance().createLabel("");
		lblStatus.setHorizontalAlignment(SwingConstants.CENTER);
		lblStatus.setForeground(Color.RED);
		lblStatus.setBounds(39, 11, 260, 14);
		contentPane.add(lblStatus);
		lblStatus.setVisible(false);
		
		if (PseudoBanco.sizeListaPessoa()==0){
			lblStatus.setText("Nao ha pessoas cadastradas");
			lblStatus.setVisible(true);
		}
		
		JButton btnBuscar = new JButton("Buscar");
		btnBuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				for(int n = 0; n < PseudoBanco.sizeListaPessoa(); n++){
					if (PseudoBanco.getCpf(n).equals(textCpf.getText())) {
						PseudoBanco.setPosicao(n);
						if (PseudoBanco.getTipo(n).equals("Instrutor")) {
							new TelaDadosInstrutor().setVisible(true);
							dispose();
						} else if (PseudoBanco.getTipo(n).equals("Cliente")) {
							new TelaDadosCliente().setVisible(true);
							dispose();
						}
						
					}else {
						lblStatus.setText("Usuario nao encontrado");
						lblStatus.setVisible(true);
						lblCpf.setText("");
					}
					
				}
			}
		});
		btnBuscar.setBounds(122, 71, 89, 23);
		contentPane.add(btnBuscar);
		
		JButton btnVoltar = new JButton("<<<");
		btnVoltar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new MenuRoot().setVisible(true);
				dispose();
			}
		});
		btnVoltar.setBounds(0, 99, 57, 23);
		contentPane.add(btnVoltar);
		
		
	}
}
